<template>
  <div class="app-container">
    <div class="search-container">
      <el-form class="form_container" ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="订单号" prop="orderId">
          <el-input style="width: 10px" v-model="queryParams.orderId" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="渠道" prop="channelIds">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.channelIds" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型" prop="bizTypes">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.bizTypes" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in bizTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderTypes">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.orderTypes" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in orderTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="平台" prop="platformIds">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.platformIds" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="车型" prop="vehicleTypeIds">
          <el-select filterable multiple clearable @keyup.enter="handleQuery" v-model="queryParams.vehicleTypeIds" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in carTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.supplierId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="用车时间">
          <el-date-picker
            style="width: 256px"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleUseTimeChange"
            v-model="useTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            style="width: 256px"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleCreatedTimeChange"
            v-model="createdTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="司机手机" prop="driverPhone">
          <el-input style="width: 256px" v-model="queryParams.driverPhone" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="乘客姓名" prop="passengerName">
          <el-input style="width: 256px" v-model="queryParams.passengerName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="乘客电话" prop="passengerPhone">
          <el-input style="width: 256px" v-model="queryParams.passengerPhone" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item prop="scoreFrom" label="评分">
          <el-input style="width: 114px !important" placeholder="请输入" v-model="queryParams.scoreFrom"></el-input>
          <div style="margin: 0 7px">至</div>
          <el-form-item prop="scoreTo">
            <el-input style="width: 114px !important" placeholder="请输入" v-model="queryParams.scoreTo"></el-input>
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
          <el-button @click="handleExport" type="warning">
            <template #icon><Edit /></template>
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="订单号" min-width="120" align="center" prop="orderId"></el-table-column>
        <el-table-column label="城市" min-width="80" align="center" prop="cityName"></el-table-column>
        <el-table-column label="路程信息" min-width="240" align="left">
          <template #default="scope">
            <div class="address_box">
              <div class="item">
                <div class="tag">起点</div>
                <div class="content">{{ scope.row.fromCityName }}-{{ scope.row.fromCityArea }}-{{ scope.row.fromName }}</div>
              </div>
              <div class="item">
                <div class="tag tag2">终点</div>
                <div class="content">{{ scope.row.toCityName }}-{{ scope.row.toCityArea }}-{{ scope.row.toName }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单来源" min-width="120" align="center">
          <template #default="scope">
            <div style="display: flex; justify-content: center">
              <div>{{ scope.row.cityName }}</div>
              <div>-{{ platFormEnum[scope.row.platformId] }}</div>
              <div>-{{ channelEnum[scope.row.channelId] }}</div>
              <div>-{{ orderTypeEnum[scope.row.orderType] }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="车型名称" min-width="100" align="center" prop="vehicleTypeName"></el-table-column>
        <el-table-column label="司机信息" align="center" min-width="80" prop="supplierName">
          <template #default="scope">
            <el-popover v-if="scope.row.driverInfo" placement="top-start" :width="200" trigger="hover">
              <div class="table_btn">
                <div>司机姓名:{{ scope.row.driverInfo.name }}</div>
                <div>司机电话:{{ scope.row.driverInfo.phone }}</div>
                <div>车牌:{{ scope.row.driverInfo.carNum }}</div>
                <div>车型:{{ scope.row.driverInfo.carBrand }}-{{ scope.row.driverInfo.carSeries }}-{{ scope.row.driverInfo.carColor }}</div>
              </div>
              <template #reference>
                <div style="color: #40a9ff; cursor: pointer">
                  {{ scope.row.driverInfo.name }}
                </div>
              </template>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" min-width="200" align="center" prop="supplierName"></el-table-column>
        <el-table-column label="用车时间" min-width="124" align="center" prop="useTime"></el-table-column>
        <el-table-column label="评价内容" min-width="124" align="center" prop="content"></el-table-column>
        <el-table-column label="评分" align="center" min-width="140" prop="supplierName">
          <template #default="scope">
            <el-rate :colors="['red', '#f7ba2a']" v-model="scope.row.score" disabled />
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});

import OrderApi from "@/api/order";
import CommonApi from "@/api/common";
import sendModal from "./components/sendModal.vue";
import logModal from "./components/logModal.vue";
import actionModal from "./components/actionModal.vue";
import moneyModal from "./components/moneyModal.vue";
import ExportApi from "@/api/exports";
import { orderStatusEnum, platFormEnum, channelEnum, orderTypeEnum, bizTypeEnum, luggageEnum, channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import { log } from "console";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();
const useTimes = ref();
const createdTimes = ref();

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

function handleExport() {
  ExportApi.rateExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "评价" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}
// 查询菜单
function handleQuery() {
  loading.value = true;
  OrderApi.rateList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleUseTimeChange(time: any) {
  if (time) {
    queryParams.useTimeFrom = `${time[0]} 00:00:00`;
    queryParams.useTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.useTimeFrom = undefined;
    queryParams.useTimeTo = undefined;
  }
}
function handleCreatedTimeChange(time: any) {
  if (time) {
    queryParams.createTimeFrom = `${time[0]} 00:00:00`;
    queryParams.createTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.createTimeFrom = undefined;
    queryParams.createTimeFrom = undefined;
  }
}

// 重置查询
function handleResetQuery() {
  queryParams.useTimeFrom = undefined;
  queryParams.useTimeTo = undefined;
  queryParams.createTimeFrom = undefined;
  queryParams.createTimeTo = undefined;
  useTimes.value = null;
  createdTimes.value = null;

  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.form_container {
  display: flex;
  flex-wrap: wrap;
}
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
